package org.example.sbringboot1_12.controller;

import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Boolean login(String userName, String password, HttpSession session){
//        if(userName==null|| "".equals(userName)){
//            return false;
//        }
        if(!StringUtils.hasLength(userName)||!StringUtils.hasLength(password)){
            return false;
        }
        if("admin".equals(userName)&&"admin".equals(password)){
            session.setAttribute("userName",userName);
            session.setAttribute("password",password);
            return true;
        }
        return false;
    }
    @RequestMapping("/getLoginUser")
    public String getLoginUser(HttpSession session) {
        if (session.getAttribute("userName") != null) {
            return (String) session.getAttribute("userName");
        }
        return "";
    }
}

